#define _CRT_SECURE_NO_WARNINGS
#include"binary_tree.h"

b_tree* b_tree_inital(b_tree* pbtree, BTDATE x)
{
	pbtree = new b_tree;
	pbtree->value = x;
	pbtree->left_tree = NULL;
	pbtree->right_tree = NULL;
	return pbtree;
}

void b_tree_preorder(b_tree* pbtree)
{
	if (!pbtree)
	{
		cout << "NULL-->";
		return;
	}
	cout << pbtree->value << "-->";
	b_tree_preorder(pbtree->left_tree);
	b_tree_preorder(pbtree->right_tree);
}

void b_tree_inorder(b_tree* pbtree)
{
	if (!pbtree)
	{
		cout << "NULL-->";
		return;
	}
	b_tree_preorder(pbtree->left_tree);
	cout << pbtree->value << "-->";
	b_tree_preorder(pbtree->right_tree);
}

void b_tree_behindorder(b_tree* pbtree)
{
	if (!pbtree)
	{
		cout << "NULL-->";
		return;
	}
	b_tree_preorder(pbtree->left_tree);
	b_tree_preorder(pbtree->right_tree);
	cout << pbtree->value << "-->";
}

void tree_size(b_tree* pbtree, int* size)
{
	if (!pbtree)
		return;
	(*size)++;
	tree_size(pbtree->left_tree,size);
	tree_size(pbtree->right_tree,size);
}

int tree_size_recursion(b_tree* pbtree)
{
	return pbtree == NULL ? 0 : tree_size_recursion(pbtree->left_tree) + tree_size_recursion(pbtree->right_tree) + 1;
}

int leaf_size_recursion(b_tree* pbtree)
{
	if (!pbtree)
		return 0;
	return pbtree->left_tree == NULL && pbtree->right_tree == NULL ? 1 : leaf_size_recursion(pbtree->left_tree) + leaf_size_recursion(pbtree->right_tree) + 0;
}

void level_order(b_tree* pbtree)
{
	queue<b_tree*>q;
	if (!pbtree)
	{
		cout << "NULL";
		return;
	}
	q.push(pbtree);
	while (!q.empty())
	{
		b_tree* newnode = q.front();
		cout << newnode->value << "  ";
		q.pop();
		if (newnode->left_tree)
      		q.push(newnode->left_tree);
		if (newnode->right_tree)
			q.push(newnode->right_tree);
	}
}